define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'newhouse/tradearea/index' + location.search,
                    add_url: 'newhouse/tradearea/add',
                    edit_url: 'newhouse/tradearea/edit',
                    del_url: 'newhouse/tradearea/del',
                    multi_url: 'newhouse/tradearea/multi',
                    import_url: 'newhouse/tradearea/import',
                    table: 'newhouse_tradearea',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title')},
                        {field: 'newhouse_tags_names', title: __('Newhouse_tags_ids'), formatter:Table.api.formatter.flag},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'admin.nickname', title: __('Admin_id')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'newhouse/tradearea/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'newhouse/tradearea/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'newhouse/tradearea/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();

            $("[data-toggle='addresspicker']").data("callback", function(res){
                //其中res则是包含了address/lat/lng等信息的JSON对象
                return $(this).attr('value',res.lng + ',' + res.lat)
            });
        },
        edit: function () {
            Controller.api.bindevent();

            $("[data-toggle='addresspicker']").data("callback", function(res){
                //其中res则是包含了address/lat/lng等信息的JSON对象
                return $(this).attr('value',res.lng + ',' + res.lat)
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                // 重复图片未处理
                // 封面上传控件
                var input_id = $('#plupload-image').data("input-id") ? $('#plupload-image').data("input-id") : "";
                var preview_id = $('#plupload-image').data("preview-id") ? $('#plupload-image').data("preview-id") : "";

                if(preview_id) {
                    $("#plupload-image").data("upload-success", function(data, ret) {
                        //这里进行后续操作
                        var imageCount = $('#p-image').children('li').length
                        if(imageCount == 1) {
                            $(this).parent('span').hide();
                        }
                    });
                    // 移除按钮事件
                    $(document.body).off("click", "#" + preview_id + " .btn-trash");
                    // 再绑定新按钮事件
                    $(document.body).on("click", "#" + preview_id + " .btn-trash", function () {
                        var that = $(this);
                        // 用这个选择有毒 必须要先移除按钮事件。
                        // var path = $("#" + preview_id + " [data-url]");
                        // 改成选择当前a标签的上一个元素获取path
                        var path = that.prev().attr('data-url');
                        // console.log(that.prev().attr('data-url'))
                        Layer.confirm('是否删除该图片？（删除后不可恢复）',
                            {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
                            function (index) {
                                var options = {
                                    url: 'newhouse/tradearea/delImage',
                                    data: {
                                        id: $("#plupload-image").data('image-id'),
                                        path: path
                                    }
                                }
                                Fast.api.ajax(options, function () {
                                    if (input_id) {
                                        $("#" + input_id).val('');
                                    }
                                    that.closest("li").remove();
                                    $("#plupload-image").parent('span').show();
                                    Layer.close(index);
                                });
                            }
                        );
                    });
                }

                // 删除图片集
                var images_input_id = $('#plupload-images').data("input-id") ? $('#plupload-images').data("input-id") : "";
                var images_preview_id = $('#plupload-images').data("preview-id") ? $('#plupload-images').data("preview-id") : "";

                if(images_preview_id) {
                    $("#plupload-images").data("upload-success", function(data, ret) {
                        //这里进行后续操作
                        var imageCount = $('#p-images').children('li').length
                        if(imageCount == 24) {
                            $(this).parent('span').hide();
                        }
                    });
                    // 移除按钮事件 必须要先移除事件
                    $(document.body).off("click", "#" + images_preview_id + " .btn-trash");
                    // 再绑定新按钮事件
                    $(document.body).on("click", "#" + images_preview_id + " .btn-trash", function () {
                        var that = $(this);
                        // 用这个选择有毒 必须要先移除按钮事件。 这个真心不好用，有时删除获取路径重复
                        // var path = $("#" + images_preview_id + " [data-url]");
                        // 改成选择当前a标签的上一个元素获取path
                        var path = that.prev().attr('data-url');
                        // console.log(that.prev().attr('data-url'))
                        Layer.confirm('是否删除该图片？（删除后不可恢复）',
                            {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
                            function (index) {
                                var options = {
                                    url: 'newhouse/tradearea/delImage',
                                    data: {
                                        id: $("#plupload-images").data('images-id'),
                                        path: path
                                    }
                                }
                                Fast.api.ajax(options, function () {
                                    if (images_input_id) {
                                        $("#" + images_input_id).val('');
                                    }
                                    that.closest("li").remove();
                                    $("#plupload-images").parent('span').show();
                                    Layer.close(index);
                                });
                            }
                        );
                    });
                }
            }
        }
    };
    return Controller;
});
